﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MyOA.Model;

namespace MyOA.DAL
{
    public class RoleManageService
    {
        DBHelper db = new DBHelper();

        /// <summary>
        /// 获取全部的角色信息
        /// </summary>
        /// <returns>角色信息</returns>
        public IList<RoleInfo> getAllRoleInfo()
        {
            IList<RoleInfo> roles = new List<RoleInfo>();

            using (SqlDataReader reader = db.getDataReader("sp_getAllRoleInfo"))
            {
                while (reader.Read())
                {
                    RoleInfo role = new RoleInfo();
                    role.RoleId = (int)reader[0];
                    role.RoleName = (string)reader[1];
                    roles.Add(role);
                }
            }
            return roles;
        }

        /// <summary>
        /// 增加角色信息
        /// </summary>
        /// <param name="roleName">要增加的角色名</param>
        /// <returns>增加是否成功</returns>
        public bool insertRoleInfo(string roleName)
        {
            if (db.getCount("sp_insertRoleInfo", new SqlParameter("@roleName", roleName)) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据角色信息ID删除角色信息
        /// </summary>
        /// <param name="roleId">角色信息ID</param>
        /// <returns>是否删除成功</returns>
        public bool deleteRoleInfoByRoleId(int roleId)
        {
            if (db.getCount("sp_DeleteRoleInfoByRoleId", new SqlParameter("@roleId", roleId)) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据ID来修改角色信息
        /// </summary>
        /// <param name="roleInfo">角色信息数据源</param>
        /// <returns>返回是否修改成功</returns>
        public bool updateRoleInfoByRoleId(RoleInfo roleInfo)
        {
            SqlParameter[] values = new SqlParameter[]
            {
                new SqlParameter("@roleId",roleInfo.RoleId),
                new SqlParameter("@roleName",roleInfo.RoleName)
            };
            if (db.getCount("sp_updateRoleInfoByRoleId", values) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
